package com.cneport.transfer.router.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cneport.transfer.router.model.NodeRelation;
import com.cneport.transfer.router.support.service.NodeRelationService;

/**
 * 节点关系管理
 * @author wudi 2012-7-18
 */
@Controller
@RequestMapping("/routeManager")
public class RouteController {
    /**
     * 节点关系service
     */
    @Autowired
    private NodeRelationService nodeRelationService;

    /** LOG 日志操作类 */
    private static final Log LOG = LogFactory.getLog(RouteController.class);

    /**
     * 获取节点关系列表
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/queryNodeRouteInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryNodeRouteInfo(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap();

        String status = request.getParameter("status");
        String start = request.getParameter("start");
        String limit = request.getParameter("limit");

        NodeRelation noderelation = new NodeRelation();
        noderelation.setStart(Integer.parseInt(start));
        noderelation.setLimit(Integer.parseInt(limit) + Integer.parseInt(start)
                + 1);
        noderelation.setStatus(status);

        List<NodeRelation> reslut = nodeRelationService
                .queryNodeRelInfo(noderelation);
        int totalProperty = nodeRelationService.conutNodeRelInfo(noderelation);

        responseMap.put("root", reslut);
        responseMap.put("totalProperty", totalProperty);

        return responseMap;
    }

    /**
     * 获取节点信息
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/getNodeRelInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getNodeRelInfo(HttpServletRequest request,
            HttpServletResponse response) {
        NodeRelation noderelation = new NodeRelation();
        Map<String, Object> responseMap = new HashMap();

        try {
            String scrNodeId = request.getParameter("scrNodeId");
            String destNodeId = request.getParameter("destNodeId");
            noderelation.setDestNodeId(destNodeId);
            noderelation.setScrNodeId(scrNodeId);

            NodeRelation noderelinfo = nodeRelationService
                    .getNodeRelInfo(noderelation);

            responseMap.put("data", noderelinfo);
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.equals(e);
        }

        return responseMap;
    }

    /**
     * 保存节点关系
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/saveNodeRelInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveNodeRelInfo(HttpServletRequest request,
            HttpServletResponse response) {
        NodeRelation noderelation = new NodeRelation();
        Map<String, Object> responseMap = new HashMap();
        String scrNodeId = request.getParameter("scrNodeId");
        String destNodeId = request.getParameter("destNodeId");
        String remark = request.getParameter("remark");
        String status = request.getParameter("status");
        String nodeRelationId = request.getParameter("nodeRelationId");
        String weight = request.getParameter("weight");
        String flag = request.getParameter("flag");

        try {
            noderelation.setScrNodeId(scrNodeId);
            noderelation.setDestNodeId(destNodeId);
            noderelation.setRemark(remark);
            noderelation.setStatus(status);
            noderelation.setNodeRelationId(nodeRelationId);
            noderelation.setWeight(Integer.parseInt(weight));

            if ("edit".equals(flag)) {
                nodeRelationService.updateNodeRelInfo(noderelation); // 成对更新节点关系
                noderelation.setScrNodeId(destNodeId);
                noderelation.setDestNodeId(scrNodeId);
                nodeRelationService.updateNodeRelInfo(noderelation);
            } else if ("add".equals(flag)) {
                nodeRelationService.insertNodeRelInfo(noderelation); // 成对增加节点关系
                noderelation.setScrNodeId(destNodeId);
                noderelation.setDestNodeId(scrNodeId);
                nodeRelationService.insertNodeRelInfo(noderelation);
            }
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }

        return responseMap;
    }

    /**
     * 删除节点关系
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/deleteNodeRelInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteNodeRelInfo(HttpServletRequest request,
            HttpServletResponse response) {
        NodeRelation noderelation = new NodeRelation();
        Map<String, Object> responseMap = new HashMap();

        try {
            String scrNodeId = request.getParameter("scrNodeId");
            String destNodeId = request.getParameter("destNodeId");
            noderelation.setScrNodeId(scrNodeId);
            noderelation.setDestNodeId(destNodeId);

            nodeRelationService.deleteNodeRelInfo(noderelation);

            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }

        return responseMap;
    }

}
